package controller;

import com.google.gson.Gson;
import entry.Books;
import pojo.MessBooks;
import service.BookService;
import service.BookServiceImpl;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@WebServlet("/book/*")
public class BookServlet extends HttpServlet {
    private BookService bookService = new BookServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) {
        //1、处理中文乱码
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset = utf-8");
        //2、获取请求路径
        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = BookServlet.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
//谈克思6666666666666666666666666666666666666
        //super.service(req, resp);
    }

    //按条件查询数据
    public void queryByBook(HttpServletRequest request, HttpServletResponse response) {
        //接收参数
        String query = request.getParameter("query");
        String str = request.getParameter("str");
        String whichPage = request.getParameter("whichPage");
        System.out.println("whichPage传回来的是" + whichPage);
        //准备参数
        Books book = new Books();
        if ("1".equals(query)){
            book.setBookname(str);
        }else if ("2".equals(query)){
            book.setBookauthor(str);
        }else if ("3".equals(query)){
            book.setBookpublish(str);
        }else {
            //全查
            /*
            *      String query = request.getParameter("query");
        String str = request.getParameter("str");
        String whichPage = request.getParameter("whichPage");
        System.out.println("whichPage传回来的是" + whichPage);
        //准备参数
        Books book = new Books();
        if ("1".equals(query)){
            book.setBookname(str);
        }else if ("2".equals(query)){
            book.setBookauthor(str);
        }else if ("3".equals(query)){
            book.setBookpublish(str);
        }else {
        * ejjdjdjdjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj*/
        }

        PageHelper pageHelper = new PageHelper();
        if (whichPage != null)
            pageHelper.setWhichPage(Integer.parseInt(whichPage));
        pageHelper.setAllNum(bookService.selectBookSize(book));
        ArrayList<Books> books = bookService.selectMatch(book, pageHelper);
        pageHelper.setList(books);

        System.out.println("共有多少条数据" + pageHelper.getAllNum());
        System.out.println("一共有几页" + pageHelper.getPageNum());
        System.out.println("一页有多少条数据" + pageHelper.getPerPage());

        Gson gson = new Gson();
        try {
            response.getWriter().println(gson.toJson(pageHelper));
            //传的是分页工具类
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //新增图书
    public void addBook(HttpServletRequest request, HttpServletResponse response){
        //获得参数
        String bookname = request.getParameter("bookname");
        String bookauthor = request.getParameter("bookauthor");
        String bookpublish = request.getParameter("bookpublish");
        String bookpage = request.getParameter("bookpage");
        String bookprice = request.getParameter("bookprice");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = sdf.format(new Date());//获取格式化的当前时间

        Books book = new Books();//参数赋值给一个心得函数
        book.setBookname(bookname);
        book.setBookauthor(bookauthor);
        book.setBookpublish(bookpublish);
        book.setBookpage(Integer.parseInt(bookpage));
        book.setBookprice(Double.parseDouble(bookprice));
        book.setCreatedate(nowTime);

        boolean tf = bookService.addBook(book);
        try {
            response.getWriter().println(tf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //删除图书
    public void delete(HttpServletRequest request, HttpServletResponse response){
        int bookid = Integer.parseInt(request.getParameter("bookid"));
        MessBooks messBooks = new MessBooks();
        //接收参数
        String query = request.getParameter("query");
        String str = request.getParameter("str");
        String whichPage = request.getParameter("whichPage");
        //准备参数
        Books book = new Books();
        if ("1".equals(query)){
            book.setBookname(str);
        }else if ("2".equals(query)){
            book.setBookauthor(str);
        }else if ("3".equals(query)){
            book.setBookpublish(str);
        }else {
            //全查
        }

        PageHelper pageHelper = new PageHelper();
        if (whichPage != null)
            pageHelper.setWhichPage(Integer.parseInt(whichPage));

        pageHelper.setAllNum(bookService.selectBookSize(book));
        ArrayList<Books> books = bookService.selectMatch(book, pageHelper);
        pageHelper.setList(books);
        messBooks.setMess(bookService.delete(bookid) ? "删除成功" : "删除失败");

        messBooks.setPageHelper(pageHelper);

        Gson gson = new Gson();
        System.out.println(messBooks);
        try {
            response.getWriter().println(gson.toJson(messBooks));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据id查找要修改的图书
    public void queryById(HttpServletRequest request, HttpServletResponse response){
        int bookid = Integer.parseInt(request.getParameter("bookid"));
        Books book = bookService.selectById(bookid);
        Gson gson = new Gson();
        try {
            response.getWriter().println(gson.toJson(book));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改图书
    public void update(HttpServletRequest request, HttpServletResponse response){
        int bookid = Integer.parseInt(request.getParameter("bookid"));
        String bookname = request.getParameter("bookname");
        String bookauthor = request.getParameter("bookauthor");
        String bookpublish = request.getParameter("bookpublish");
        int bookpage = Integer.parseInt(request.getParameter("bookpage"));
        double bookprice = Double.parseDouble(request.getParameter("bookprice"));
        String createdate = request.getParameter("createdate");
        Books book = new Books(bookid, bookname, bookauthor, bookpublish, bookpage, bookprice, createdate);

        boolean tf = bookService.updateBook(book);
        try {
            response.getWriter().println(tf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
